<?php

class HmDeliveryVolumeController extends Fw_Controller_Action
{
	public function init()
	{
		$this->_helper->layout->disableLayout();
		$this->initSession();
	}

	public function indexAction()
	{
		$offer_id = (int)$this->_request->getParam('offer_id');
		$model = $this->_getModel('HmDeliveryVolume');
		$view_list = $this->_model;
		$query = $view_list->select()
							->setIntegrityCheck(false)
							->from($view_list)
							->join('hm_delivery_place', 'hm_delivery_place.delivery_place_id = hm_delivery_volume.delivery_place_id')
							->join('hm_delivery_type', 'hm_delivery_type.delivery_type_id = hm_delivery_volume.delivery_type_id')
							->where('offer_id='.$offer_id);
		$list = $view_list->fetchAll($query);
		$this->view->rowCount = count($list);
		$this->view->view_list = $list;
	}
	
	public function addAction()
	{
		$this->view->offer_id = (int)$this->_request->getParam('offer_id');
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if ($formData) {
				
				$model = $this->_getModel('HmDeliveryVolume');
				$add   = $this->_model;
				
				$row                    = $add->createRow();
				$row->volume            = $this->_request->getPost('volume');
				$row->delivery_date     = $this->_request->getPost('delivery_date');
				$row->delivery_place_id = $this->_request->getPost('delivery_place_id');
				$row->delivery_type_id  = $this->_request->getPost('delivery_type_id');
				$row->offer_id          = $this->_request->getPost('offer_id');
				$row->save();
				
				return $this->_helper->redirector('index');
			}
		}
		require_once APPLICATION_PATH . '/models/HmDeliveryPlace.php';
		$placeTable = new Model_HmDeliveryPlace();
		$place_list = $placeTable->fetchAll();
		$placeRowCount = count($place_list);
		
		$this->view->place_list    = $place_list;
		$this->view->placeRowCount = $placeRowCount;
		
		require_once APPLICATION_PATH . '/models/HmDeliveryType.php';
		$typeTable = new Model_HmDeliveryType();
		$type_list = $typeTable->fetchAll();
		$typeRowCount = count($type_list);
		
		$this->view->type_list    = $type_list;
		$this->view->typeRowCount = $typeRowCount;
	}
	
	public function editAction()
	{
		$model   = $this->_getModel('HmDeliveryVolume');
		$edit    = $this->_model;
		$delivery_volume_id = (int)$this->_request->getParam('delivery_volume_id');
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
            if ($formData) {
				$delivery_volume_id = (int)$this->_request->getPost('delivery_volume_id');
                
				$row = $edit->fetchRow('delivery_volume_id='.$delivery_volume_id);
                $row->volume         = $this->_request->getPost('volume');
                $row->delivery_date  = $this->_request->getPost('delivery_date');
                $row->delivery_place = $this->_request->getPost('delivery_place');
                $row->delivery_type  = $this->_request->getPost('delivery_type');
                $row->offer_id       = $this->_request->getPost('offer_id');
                $row->save();
                
                return $this->_helper->redirector('index');
            }
        }
		$this->view->edit = $edit->fetchRow('delivery_volume_id='.$delivery_volume_id);
		require_once APPLICATION_PATH . '/models/HmDeliveryPlace.php';
		$placeTable = new Model_HmDeliveryPlace();
		$place_list = $placeTable->fetchAll();
		$placeRowCount = count($place_list);
		
		$this->view->place_list    = $place_list;
		$this->view->placeRowCount = $placeRowCount;
		
		require_once APPLICATION_PATH . '/models/HmDeliveryType.php';
		$typeTable = new Model_HmDeliveryType();
		$type_list = $typeTable->fetchAll();
		$typeRowCount = count($type_list);
		
		$this->view->type_list    = $type_list;
		$this->view->typeRowCount = $typeRowCount;
	}
	
	public function showAction()
	{
		$delivery_volume_id = (int)$this->_request->getParam('delivery_volume_id');
				
		$model = $this->_getModel('HmDeliveryVolume');
		$show = $this->_model;
		$this->view->show = $show->fetchRow('delivery_volume_id='.$delivery_volume_id);
	}
	
	public function deleteAction()
	{
		if ($this->_request->isPost()) {
            $delivery_volume_id = (int)$this->_request->getPost('delivery_volume_id');
            $del = $this->_request->getPost('del');
            if ($del == 'Yes' && $delivery_volume_id > 0) {
                
				$model = $this->_getModel('HmDeliveryVolume');
				$remove = $this->_model;
				
                $where = 'delivery_volume_id = ' . $delivery_volume_id;
                $remove->delete($where);
            }
            return $this->_helper->redirector('index');
        } else {
            $delivery_volume_id = (int)$this->_request->getParam('delivery_volume_id');
            if ($delivery_volume_id > 0) {
                
				$model = $this->_getModel('HmDeliveryVolume');
				$remove = $this->_model;
				
                $this->view->remove = $remove->fetchRow('delivery_volume_id='.$delivery_volume_id);
            }
        }
	}
	/*
    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . 'models/HmDeliveryVolume.php';
            $this->_model = new Model_HmDeliveryVolume();
        }
        return $this->_model;
    }
	*/	
}